package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.PayManage;
import cn.wolfcode.crm.query.PayManageQueryObject;
import cn.wolfcode.crm.service.IPayManageService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("payManage")
public class PayManageController {
    @Autowired
    public IPayManageService payManageService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"payManage:view", "支出列表"}, logical = Logical.OR)
    public String view() {

        return "payManage/payManage";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(PayManageQueryObject qo) {

        return payManageService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<PayManage> selectAll() {
        return payManageService.selectAll();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"payManage:delete", "支出删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            payManageService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"payManage:saveOrUpdate", "支出新增或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(PayManage dept) {
        try {
            if (dept.getId() != null) {
                payManageService.updateByPrimaryKey(dept);
            } else {
                payManageService.insert(dept);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"payManage:changeState", "支出"}, logical = Logical.OR)
    public JSONResult changeState(Long id) {
        try {
            payManageService.changState(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}

